<?php

    include("conn.php");

    $t1 = microtime(true);

    $from=$_REQUEST ["from"]; ///  uid自己。 以后要换token _GET  _POST

    $to=$_REQUEST ["to"];

    $content=$_REQUEST ["content"];

	//http://192.168.1.100/s/send_msg.php?from=22&to=23&content=jbmadddo啊

    // from 发送content聊天 to 

    //参数验证
    if($from==NULL || $to==NULL ||  $content==NULL ||  strlen($content) <1 ){
        $ret->code=-1; 
		$ret->desc="no param"; 
		die( json_encode($ret) );  
    }

	//还要验证好友关系。。。

	
		$s2="insert into  tb_friendmsg  (createTime,send,_to,content) values ( now(), $from,$to,'$content' ); ";
		
		//echo $s2;

		$jb->code=0;

		//执行
		mysql_query($s2, $conn)  or die(  json_encode($jb) );

		mysql_close();

        // echo "   $s2" , mysql_affected_rows() ;

           
        $t2 = microtime(true);
     
		$jb->code=1;
        $jb->test=  "耗时".round($t2-$t1,6)."秒"; 
        echo json_encode($jb);

 

		
	//还要更新对方的会话表。插入or更新。 
	$strsql="select * from tb_session where uid=$to and friendID=$from  "; 

	// 执行sql查询
	$result=mysql_query($strsql, $conn);
	   
	$rows=mysql_fetch_assoc($result);
	if(!$rows)
	{
		 
			//不存在，
		$strsql="insert  into tb_session (uid , friendID , lastContent , lastTime ,state,type ) values( $to ,  $from , '$content' ,now() , -1 ,1) "; 

		// echo $strsql;

		mysql_query($strsql, $conn);//   or die(  json_encode($jb) );
		 
	}else{
		 //有，则更新

		$strsql="update tb_session set lastContent='$content', lastTime=now(),state=state-1 where uid=$to and  friendID=$from  "; 

		// echo $strsql;

		mysql_query($strsql, $conn);//  or die(  json_encode($jb) );
		 
	}


?>
